﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interactivity;

namespace Albatross.WPF.Common.Views {
	//the behavior will send the current view service to the current data context
	public class ViewServiceDispatcher : Behavior<FrameworkElement> {
		protected override void OnAttached() {
			((FrameworkElement)AssociatedObject).DataContextChanged += ViewServiceDispatcher_DataContextChanged;
			base.OnAttached();
		}

		protected override void OnDetaching() {
			((FrameworkElement)AssociatedObject).DataContextChanged -= ViewServiceDispatcher_DataContextChanged;
			ViewService = null;
			base.OnDetaching();
		}

		void ViewServiceDispatcher_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
			ViewService = (IViewService)AssociatedObject.GetValue(Attached.ViewServiceProperty);
		}

		
		public const string ViewServicePropertyName = "ViewService";
		public static readonly DependencyProperty ViewServiceProperty = DependencyProperty.Register(ViewServicePropertyName, typeof(IViewService), typeof(ViewServiceDispatcher), new PropertyMetadata());
		public IViewService ViewService {
			get { return (IViewService)GetValue(ViewServiceProperty); }
			set { SetValue(ViewServiceProperty, value); }
		}
	}
}
